package framework.elements;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

public class ElementList extends BaseElement {

    private String elementTemplate = null;

    public ElementList(final By locator, final String name) {
        super(locator, name);
    }

    public ElementList(
            final By locator,
            final String name,
            final String xPathTemplateToLink) {
        super(locator, name);
        elementTemplate = xPathTemplateToLink;
    }

    @Override
    protected final String getElementType() {
        return "List";
    }

    public final List<WebElement> getListElements() {
        try {
            List<WebElement> list = browser.getDriver().findElements(locator);
            List<WebElement> res = new ArrayList<WebElement>();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).isDisplayed()) {
                    res.add(list.get(i));
                }
            }
            return res;
        } catch (Exception e) {
            return new ArrayList<WebElement>();
        }
    }
    
    public final List<String> getListElementTexts() {
        try {
            List<WebElement> list = browser.getDriver().findElements(locator);
            List<String> res = new ArrayList<String>();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).isDisplayed()) {
                    res.add(list.get(i).getText());
                }
            }
            return res;
        } catch (Exception e) {
            return new ArrayList<String>();
        }
    }

    public final boolean hasListElement(final String linkText) {
        Link element;
        if (elementTemplate == null) {
            element = new Link(By.linkText(linkText), linkText);
        } else {
            element = new Link(By.xpath(String
                    .format(elementTemplate, linkText)), linkText);
        }
        return element.isPresent();
    }

    public final void selectListElement(String linkText) {
        Link element;
        if (elementTemplate == null) {
            element = new Link(By.linkText(linkText), linkText);
        } else {
            element = new Link(By.xpath(String
                    .format(elementTemplate, linkText)), linkText);
        }
        element.clickAndWait();
    }

    public final String getListElement(int index) {
        List<WebElement> templist = getListElements();
        Assert.assertTrue(templist.size() > 0, "List is empty");
        Assert.assertFalse(templist.size() < index, "Out of bounds");
        return templist.get(index).getText();
    }

    public final void selectListElement(int index) {
        List<WebElement> templist = getListElements();
        Assert.assertTrue(templist.size() > 0, "List is empty");
        Assert.assertFalse(templist.size() < index, "Out of bounds");
        selectListElement(templist.get(index).getText());
    }

    public final void selectRandomElement() {
        selectRandomElement(100500);
    }

    /**
     * Selecting item from 1 to maxIndex.
     * @param maxIndex
     */
    public void selectRandomElement(int maxIndex) {
        List<WebElement> templist = getListElements();
        Assert.assertTrue(templist.size() > 0, "List is empty");
        int r = Math.abs(new Random().nextInt()) % templist.size() % maxIndex;
        selectListElement(templist.get(r).getText());
    }
}
